////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                            //
//                                                         WNProject                                                          //
//                                                                                                                            //
//            This file is distributed under the BSD 2-Clause open source license. See License.txt for details.               //
//                                                                                                                            //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef __WN_FILE_IO_FILE_H__
#define __WN_FILE_IO_FILE_H__

#include "WNCore/inc/WNPlatform.h"
#include "WNCore/inc/WNTypes.h"

#ifdef _WN_WINDOWS
    #include <windows.h>
#else
    #include <stdio.h>
#endif

namespace WNFileIO {
    class WNFile {
    public:
        WNFile();
        ~WNFile();

        enum WNFileMode {
            WN_NONE         = 0,
            WN_READ         = 1,
            WN_WRITE        = (1 << 1),
            WN_EXCLUSIVE    = (1 << 2),
            WN_CREATE       = (1 << 3),
            WN_CLOBBER      = (1 << 4),
            WN_STREAM       = (1 << 5)
        };

        enum WNFileError {
            #include "WNCore/inc/WNErrors.h"
            WN_FILE_BAD_MODE,
            WN_FILE_TOO_LARGE,
            WN_FILE_BAD_LOCK
        };

        WNFileError     OpenFile(const WN_CHAR* _name, WN_UINT32 _mode );
        WN_CHAR*        GetDataBuffer();
        WN_BOOL         CommitFileBuffer();
        WN_CHAR*        ResizeDataBuffer(WN_SIZE_T _size );
        static WN_BOOL  DoesFileExist(const WN_CHAR* _name );
        WN_SIZE_T       ReadData(WN_CHAR* outBuffer, WN_SIZE_T _amount);
        WN_SIZE_T       WriteData(WN_CHAR* inBuffer, WN_SIZE_T _amount);
        WN_BOOL         IsValid();

    private:
        WN_UINT32       mFileMode;
        WN_SIZE_T       mMaxBufferSize;
        WN_SIZE_T       mFileSize;
        WN_CHAR*        mFileBuffer;
        WN_BOOL         mIsFileOpen;
        WN_BOOL         mStream;
        WN_BOOL         mIsValid;

        #ifdef _WN_WINDOWS
            HANDLE      mFileHandle;
        #elif defined _WN_LINUX
            FILE*       mFileHandle;
        #endif
    };
}

#endif // __WN_FILE_IO_FILE_H__
